<?php

namespace App\Console\Commands;

use App\Models\Child;
use App\Models\Device;
use App\Models\Game;
use App\Models\History;
use App\Models\User;
use Illuminate\Console\Command;
use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Illuminate\Support\Facades\Log;

class MakeData extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    //protected $signature = 'command:name';
    protected $signature = 'db:makedata';
    /**
     * The console command description.
     *
     * @var string
     */
    //protected $description = 'Command description';
    protected $description = 'db make data';

    protected $process;
    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

/**
* Execute the console command.
*
* @return mixed
*/
    public function handle()
    {
        $agency_id = 1;

        $children = Child::where("agency_id", $agency_id)->pluck("id");
        $games = Game::all();
        $devices = Device::where("agency_id", $agency_id)->pluck("id");

        $teachers = User::where("agency_id", $agency_id)->pluck("id");
        if (empty($teachers)){
            echo "no teachers";
            return false;
        }

        $children->each(function(&$item) use ($agency_id, $games, $devices, $teachers){


            foreach ($devices as $device_id){
                foreach ($games as $game){

                    $countnum = rand(1, 4); //添加随机数量记录
                    $countnum = $countnum * (15-$game->grade_id*3);
                    $data = [];
                    for ($i=0;$i<$countnum;$i++){
                        $time = time() - rand(1296000,2592000);// 一个月前到半个月前之间
                        $use_time = rand(5,20);
                        $start_star = rand(0,2);
                        $end_star = $start_star + rand(0,1);

                        $data = ['child_id'=>$item,
                            'game_id'=>$game->id,
                            'grade_id'=>$game->grade_id,
                            'category_id'=>$game->category_id,
                            'device_id'=>$device_id,
                            'agency_id'=>$agency_id,
                            'user_id'=>$teachers->random(),
                            'start_time'=>date('Y-m-d H:i:s', $time),
                            'end_time'=>date('Y-m-d H:i:s', $time + $use_time),
                            'use_time'=>$use_time,
                            'start_star'=>$start_star,
                            'end_star'=>$end_star,
                            'result'=>rand(0,1),
                        ];
                        History::create($data);
                    }
                }
            }
        });

    }
}
